<?php
namespace app\index\controller;

use app\common\Base;
use app\index\model\Business;
use app\index\model\User;
use app\index\validate\BusinessRegister;
use app\common\Upload;
use think\Exception;
use think\Request;

class EnterStore extends Base
{
    //店铺入驻
    //传值:businessType,businessImg,businessLicenceImg,licenceupImg,licencedownImg,businessName,phone,deliverMoney,startDeliver,startTime,endTime,provide,city,county,street,businessAddress,businessDesc
    public function registerStore(Request $request,User $user,Business $business,BusinessRegister $businessRegister){
        try{
            if (!$businessRegister->check($request->post())){
                return responseJson(Base::FAIL,$businessRegister->getError());
            }
            // TODO 将获取缓存信息放在中间件里
            $arr = $request->cacheInfo;

            //判断是否已入驻
            $storeNum = $business->findStore($arr['id']);
            if ($storeNum >= 1){
                return responseJson(Base::FAIL,'你已经入驻过店铺了');
            }

            // TODO 以下文件确保有文件流信息
            //获取图片信息
            //businessImg店铺主照片
            $businessImg = $request->file('businessImg')->getInfo();
            //businessLicenceImg店铺营业照片
            $businessLicenceImg = $request->file('businessLicenceImg')->getInfo();
            //licenceupImg法人证件正面
            $licenceupImg = $request->file('licenceupImg')->getInfo();
            //licencedownImg法人证件反面
            $licencedownImg = $request->file('licencedownImg')->getInfo();
            $upload = new Upload();
            
            $domain_businessImg = 'http://hungry.wistudy.xyz/' . ($upload->upload($businessImg['tmp_name'],time() . '.' . $businessImg['name']))['key'];

            $domain_businessLicenceImg = 'http://hungry.wistudy.xyz/' . ($upload->upload($businessLicenceImg['tmp_name'],time() . '.' . $businessLicenceImg['name']))['key'];

            $domain_licenceupImg = 'http://hungry.wistudy.xyz/' . ($upload->upload($licenceupImg['tmp_name'],time() . '.' . $licenceupImg['name']))['key'];

            $domain_licencedownImg = 'http://hungry.wistudy.xyz/' . ($upload->upload($licencedownImg['tmp_name'],time() . '.' . $licencedownImg['name']))['key'];

            //将参数全传进去(入驻)
            $data = $business->enterStoreInformation($arr['id'],$domain_businessImg,$domain_businessLicenceImg,$domain_licenceupImg,$domain_licencedownImg,$user->findPassword($arr['id']),$request->post('businessType'),$request->post('businessName'),$request->post('phone'),$request->post('deliverMoney'),$request->post('startDeliver'),$request->post('startTime'),$request->post('endTime'),$request->post('provide'),$request->post('city'),$request->post('county'),$request->post('street'),$request->post('businessAddress'),$request->post('businessDesc'));

            //判断是否传值成功
            if ($data != 1){
                return responseJson(Base::FAIL,'店铺入驻失败');
            }
            return responseJson(Base::OK,'店铺入驻成功');

        }catch (Exception $e){
            throw $e;
        }
    }
}